#ifndef URLHOOK_HH
#define URLHOOK_HH

#include <sys/timeb.h>
#include <sys/types.h>
#include <winsock.h>
#include <windows.h>
#include <conio.h>
#include "TypedCallback.hh"
#include <vector>

using namespace std;

#define HTML_REPLY_OK_PRE         "HTTP/1.0 200 OK\n Content-Type: text/html\n<HTML>\n\n<HEAD>%s<TITLE>Web Motor Status Server </TITLE></HEAD>\n<H1> Web Motor Status Server </H1>\n<H2> %s%s"
#define HTML_REPLY_REFRESH        "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"%d; URL=http://%s:%d/Status\" >"
#define HTML_REPLY_OK_POST " </H2>\n</HTML>\n\n"

#define MYPORT 3490

/*! \file This file defines a cheap way to install a web server on a specified port
 * It watches over a few URLS and calls the callback object when a user tries to open
 * the URL. We hook onto 127.0.0.1:port
 */
typedef struct cmd_cb_s_{
    char url_str[80];
    char url_param[550];
    TypedCallbackInterface *cb;
} cmd_cb_t;

class UrlHook {
private:
    vector<cmd_cb_t *> cb_vec;
    SOCKET sockfd;
    SOCKET currentfd;
    char hostname[200];
	unsigned short server_port;

private:
	int dispatch_url_cb(char *);
public:
    UrlHook(unsigned short port = MYPORT);
    int set_url_cb(char *url, TypedCallbackInterface *cb);
    int reply_browser(char *reply, bool refresh = false, int time = 1);
    int evt_loop(void);
};

#endif // URLHOOK_HH
